package com.pojoin.cms.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;

import com.pojoin.cms.domain.Resource;
import com.pojoin.cms.domain.Role;
import com.pojoin.cms.domain.User;
import com.pojoin.cms.domain.form.UserForm;
import com.pojoin.cms.exception.CmsException;
import com.pojoin.cms.face.UserFace;
import com.pojoin.cms.face.UserFaceFactory;
import com.pojoin.cms.service.RoleService;
import com.pojoin.cms.service.UserService;

@SuppressWarnings("serial")
public class UserAction extends PagerAction {

	private UserService userService;
	private UserFaceFactory userFaceFactory;
	private RoleService roleService;

	private List<User> list;

	private List<Role> roleList;

	private UserForm user;
	
	private List<Resource> resourceList;

	public List<Resource> getResourceList() {
		return resourceList;
	}

	public void setResourceList(List<Resource> resourceList) {
		this.resourceList = resourceList;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public UserForm getUser() {
		return user;
	}

	public void setUser(UserForm user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserFaceFactory getUserFaceFactory() {
		return userFaceFactory;
	}

	public void setUserFaceFactory(UserFaceFactory userFaceFactory) {
		this.userFaceFactory = userFaceFactory;
	}

	public List<User> getList() {
		return list;
	}

	public void setList(List<User> list) {
		this.list = list;
	}

	/**
	 * 请求添加用户表单
	 * 
	 * @return
	 */
	public String willAdd() {
		this.roleList = this.roleService.getAllRoles();
		return this.success("user/add.jsp");
	}

	/**
	 * 添加用户
	 * @return
	 * @throws CmsException
	 */
	public String add() throws CmsException {

		User u = this.userService.addUser(this.user);
		if (u == null) {
			this.msg = "用户已经存在";
			return this.willAdd();
		}

		UserFace userFace = this.userFaceFactory.getUserFaceByUserId(u.getUserId());
		userFace.resetRoles(this.user.getSelectRoleIds());
		userFace.flush();
		return this.list();
	}
	
	/**
	 * 删除对象
	 * @return
	 * @throws CmsException
	 */
	public String del() throws CmsException{
		UserFace userFace = this.userFaceFactory.getUserFaceByUserId(user.getUserId());
		userFace.delete();
		return this.list();
	}
	
	/**
	 * 请求更新用户
	 * @return
	 */
	public String load(){
		this.roleList = this.roleService.getAllRoles();
		User u = this.userService.fingById(this.user.getUserId());
		BeanUtils.copyProperties(u, this.user);
		Set<Role> roles = this.user.getRoles();
		String roleIds = "";
		for(Role r : roles){
			roleIds = r.getRoleId() + ",";
		}
		this.user.setSelectRoleIds(roleIds);
		return this.success("user/update.jsp");
	}
	
	/**
	 * 更新用户
	 * @return
	 * @throws CmsException 
	 */
	public String update() throws CmsException{
		UserFace uf = this.userFaceFactory.getUserFaceByUserId(this.user.getUserId());
		uf.update(this.user);
		uf.flush();
		return this.list();
	}

	/**
	 * 获取列表
	 * 
	 * @return
	 */
	public String list() {
		this.list = this.userService.getAllUsers(this.pager);
		this.pager.setAction("v/userAction/list");
		return this.success("user/list.jsp");
	}
	
	/**
	 * 获取当前用户的所有资源
	 * @return
	 */
	public String getCurentUserResources(){
		User u= this.userService.getUserByUserName(this.getCurrentUserName());
		this.resourceList = new ArrayList<Resource>();
		Set<Role> roleSet = u.getRoles();
		for(Role role : roleSet){
			Set<Resource> resourceSet = role.getResources();
			for(Resource resource : resourceSet){
				this.resourceList.add(resource);
			}
			resourceSet = null;
		}
		roleSet = null;
		return this.success("MenuTree.jsp");
	}

}
